titles1 = [];
titles2 = [];
titles3 = [];
titleIds = [];
id = null;


function addtestpaper() {
    /*console.log("进入add");
    console.log($("#test_name").val());
    console.log($("#test_subject").val());
    console.log($("#sum_gader").val());
    console.log($("#test_time").val());
    console.log($("#choose_number").val());
    console.log($("#choose_gader").val());
    console.log($("#completion_gader").val());
    console.log($("#grade").val());
    console.log($("#completion_num").val());
    console.log($("#recognized_num").val());
    console.log($("#recognized_gader").val());
    console.log($("#paper_rollOfPeople").val());*/

    if (parseInt($("#sum_gader").val()) != (parseInt($("#choose_gader").val()) + parseInt($("#completion_gader").val()) + parseInt($("#recognized_gader").val()))) {
        alert($("#sum_gader").val() + "" + $("#choose_gader").val() + "" + $("#completion_gader").val() + "" + $("#recognized_gader").val());
        alert("分数设置不合理，题型总分和总分不相等,请重新设置");
        $("#sum_gader").val(parseInt($("#choose_gader").val()) + parseInt($("#completion_gader").val()) + parseInt($("#recognized_gader").val()));
        return;
    }
    if (parseInt($("#sum_title_num").val()) != (parseInt($("#choose_number").val()) + parseInt($("#completion_num").val()) + parseInt($("#recognized_num").val()))) {
        alert("总题目数量和已出题目数量不相等,准备自动更新总题目数量,并重新提交");
        $("#sum_title_num").val($("#choose_number").val() + $("#completion_num").val() + $("#recognized_num").val());
        return;
    }

    $.get('/testPaper/returnData', function (data) {
        /*$("#choose_title_sd").val(data.data_length);*/
        console.log('/testPaper/returnData');
        console.log(data);
        titles1 = data.titleids1;
        titles2 = data.titleids2;
        titles3 = data.titleids3;
        stringToInt(titles1);
        stringToInt(titles2);
        stringToInt(titles3);
        console.log(titleIds)
        localStorage.setItem("titleIds", titleIds);
    }, 'json');

    $.get('/testPaper/getTitleId', {
        test_name: $("#test_name").val(),
        grade: $("#grade").val(),
        test_subject: $("#test_subject").val(),
        sum_gader: $("#sum_gader").val(),
        sum_title_num: $("#sum_title_num").val(),
        choose_number: $("#choose_number").val(),
        choose_gader: $("#choose_gader").val(),
        completion_gader: $("#completion_gader").val(),
        completion_num: $("#completion_num").val(),
        recognized_num: $("#recognized_num").val(),
        recognized_gader: $("#recognized_gader").val(),
        paper_rollOfPeople: $("#paper_rollOfPeople").val()
    }, function (data) {
        console.log("试卷添加成功，" + data.result);
        localStorage.setItem("id", data.id);
        id = data.id;
        // alert(data.result);
    }, 'json');


    localStorage.clear();
}


function OnInput(event) {
    console.log("文本框被改变1");
    localStorage.setItem("test_name", $("#test_name").val());
    console.log("$(\"#test_name\").val()" + $("#test_name").val());
    console.log(localStorage.getItem("test_name", $("#test_name").val()));
    localStorage.setItem("test_subject", $("#test_subject").val());
    localStorage.setItem("paper_rollOfPeople", $("#paper_rollOfPeople").val());
    localStorage.setItem("sum_gader", $("#sum_gader").val());
    localStorage.setItem("sum_title_num", $("#sum_title_num").val());
    localStorage.setItem("test_start_time", $("#test_start_time").val());
    localStorage.setItem("test_time", $("#test_time").val());
    localStorage.setItem("choose_number", $("#choose_number").val());
    localStorage.setItem("choose_gader", $("#choose_gader").val());
    localStorage.setItem("completion_gader", $("#completion_gader").val());
    localStorage.setItem("grade", $("#grade").val());
    localStorage.setItem("completion_num", $("#completion_num").val());
    localStorage.setItem("recognized_num", $("#recognized_num").val());
    localStorage.setItem("recognized_gader", $("#recognized_gader").val());
    console.log("出去了");

}


$(document).ready(function () {
    getGrade();
    getModel("SUBJECT");
    $("#test_name").val(localStorage.getItem("test_name"));
    console.log("read:" + localStorage.getItem("test_name"));
    $("#test_subject").val(localStorage.getItem("test_subject"));
    $("#sum_gader").val(localStorage.getItem("sum_gader"));
    $("#sum_title_num").val(localStorage.getItem("sum_title_num"));
    $("#choose_number").val(localStorage.getItem("choose_number"));
    $("#choose_gader").val(localStorage.getItem("choose_gader"));
    $("#completion_gader").val(localStorage.getItem("completion_gader"));
    $("#grade").val(localStorage.getItem("grade"));
    $("#completion_num").val(localStorage.getItem("completion_num"));
    $("#recognized_num").val(localStorage.getItem("recognized_num"));
    $("#recognized_gader").val(localStorage.getItem("recognized_gader"));
    $("#paper_rollOfPeople").val(localStorage.getItem("paper_rollOfPeople"));
    $("#sum_title_num").val(parseInt(localStorage.getItem("completion_num")) +
        parseInt(localStorage.getItem("recognized_num")) + parseInt(localStorage.getItem("choose_number")));
    $("#recognized_gader").val($("#sum_gader").val() - $("#choose_gader").val() - $("#completion_gader").val());
});

//获取科目、题型
function getModel(obj) {
    $.get('/testPaper/getByModel',{select_type:obj},function (data) {
        console.log(data);
        html ='';
        for (let i = 0; i < data.result.length; i++) {
            html += '<option>'+data.result[i].select_name+'<option>';
        }
        $("#test_subject").html(html);
    },'json');
}

//获取年纪
function getGrade() {
    var date = new Date();
    var year = date.getFullYear();
    console.log(year);
    let html = '<option>' + year + '</option>\n' +
        '            <option>' + (year - 1) + '</option>\n' +
        '            <option>' + (year - 2) + '</option>\n' +
        '            <option>' + (year - 3) + '</option>';
    $("#grade").html(html);
}

//将字符串类型的数字装换成数字类型
function stringToInt(obj) {
    for (i = 0; i < obj.length; i++) {
        var number = parseInt(obj[i]);
        titleIds.push(number);
        console.log(number);
    }
}

//根据试题删除试题
function deleteTitle(titleid) {
    $.get('/Question_paper/delete', {question_id: titleid}, function (data) {
        alert(data.result);
    })
}

//查看已选试题
function displayTitle() {
    var html1 = '第一大题、单项选择题';
    var html2 = '第二大题、填空题';
    var html3 = '第三大题、判断题';
    var choNo = 1;
    var comNo = 1;
    var rolNo = 1;
    for (i = 0; i < titleIds.length; i++) {
        console.log(titleIds.length);
        $.get('/titleQuestion/displayPaper', {question_id: titleIds[i]}, function (data) {
            console.log("displayids:");
            console.log(titleIds[i]);
            console.log(data);
            if (data.display[0].question_type == "单选题") {
                var question_option = JSON.parse(data.display[0].question_option);
                html1 += '<div style="background-color: #b2dba1"><b><p><span>' + choNo + '、</span>' + data.display[0].question_subject + '</p></b><br>\n' +
                    '            &nbsp;&nbsp; &nbsp;&nbsp;<p>A：' + question_option.A + '</p> ' +
                    '            &nbsp;&nbsp; &nbsp;&nbsp;<p>B：' + question_option.B + '</p>' +
                    '            &nbsp;&nbsp; &nbsp;&nbsp;<p>C：' + question_option.C + '</p>' +
                    '            &nbsp;&nbsp; &nbsp;&nbsp;<p>D：' + question_option.D + '</p>' +
                    '<input type="button" value="删除该题"  class="btn btn-default" onclick="deleteTitle(data.display[i].question_id)">' +
                    '</div><br>';
                choNo++;
            } else if (data.display[0].question_type == "填空题") {
                html2 += '<div style="background-color: #b2dba1"><b><p><span>' + comNo + '、</span>' + data.display[0].question_subject + '</p></b><br/>\n' +
                    '            &nbsp;&nbsp; &nbsp;&nbsp;<input type="text" placeholder="请将答案填入文本框中"><br>' +
                    '<input type="button" value="删除该题"  class="btn btn-default" onclick="deleteTitle(data.display[i].question_id)">' +
                    '</div><br>';
                comNo++;
            } else if (data.display[0].question_type == "判断题") {
                var question_option = JSON.parse(data.display[0].question_option);
                html3 += '<div style="background-color: #b2dba1"><b><p><span>' + rolNo + '、</span>' + data.display[0].question_subject + '</p></b><br/>\n' +
                    '            &nbsp;&nbsp;&nbsp;&nbsp;<span>T:' + question_option.T + '</span> &nbsp;&nbsp;&nbsp;&nbsp;<span>F:' + question_option.F + '错误</span>' +
                    '<input type="button" value="删除该题"  class="btn btn-default" onclick="deleteTitle(data.display[i].question_id)">' +
                    '</div><br>';
                rolNo++;
            }
            $("#chooseTitle").html(html1);
            $("#completionTitle").html(html2);
            $("#recognizedTitle").html(html3);
        }, 'json');
    }
}

function paper() {
    window.location.href = "displaytestpaper.html";
}

//添加试题id
function addQuestion() {
    for (let i = 0; i < titleIds.length; i++) {
        console.log("titleids  for循环");
        console.log(titleIds[i]);
        if (titleIds[i] == null || localStorage.getItem("id") == "") {
            alert("请先点击\"设置\"");
            return;
        }
        $.get('/questionPaper/addTitle', {
            question_id: titleIds[i],
            id: localStorage.getItem("id")
        }, function (data) {
            console.log("添加试题数量" + data.result);
        }, 'json');
    }
    alert("试卷保存成功");
    window.location.href = "displaytestpaper.html";
}
